package com.ari.actions;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import ari.com.eft.epp.namespace.EPPSupportedExtensions;
import ari.com.eft.utils.EFTFileReader;

import com.devicemantra.coordinate.dao.Settings;
import com.devicemantra.coordinate.dao.SettingsDAO;

public class EPPDServerSettingsAction {

	private String friendlyName;
	private String ipAddress;
	private String portNumber;
	private String username;
	private String password;
	private String outCome;
	private File jksFile;
	private String jksFileName;
	private String jksFileContentType;
	private String extensions;

	// DAO injection
	SettingsDAO settingsDAO;

	public String getJksFileContentType() {
		return jksFileContentType;
	}

	private File pcksFile;
	private String pcksFileName;
	private String pcksFileContentType;

	static final Logger logger = Logger
			.getLogger(EPPDServerSettingsAction.class);

	public EPPDServerSettingsAction() {
		ipAddress = "";
		portNumber = "";
		username = "";
		password = "";
		outCome = "Successfully uploaded the file";
	}

	@Transactional
	public String execute() {
		if ("".equals(ipAddress)) {
			return "view";
		}

		/* Read the bytes from the file into memory */
		byte[] pcksFileBytes = null;
		byte[] jksFileBytes = null;
		try {
			pcksFileBytes = EFTFileReader.readFileIntoByteArray(pcksFile);
			jksFileBytes = EFTFileReader.readFileIntoByteArray(jksFile);
		} catch (final IOException e) {
			e.printStackTrace();
		}

		try {
			Thread.sleep(1000 * 5);
		} catch (final InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		outCome = "Successfully done operation";
		final Settings settings = new Settings();
		try {
			settings.setFriendlyName(friendlyName);
			settings.setEppdIp(ipAddress);
			settings.setEppdPort(portNumber);
			settings.setEppdUsername(username);
			settings.setEppdPassword(password);
			settings.setJksFileBytes(jksFileBytes);
			settings.setPcksFileBytes(pcksFileBytes);
			settings.setJksFileName(jksFileName);
			settings.setPcksFileName(pcksFileName);

			/*
			 * Test EPP Extensions \\ final List<EPPExtension> eppExts = new
			 * ArrayList<EPPExtension>(); EPPExtension eppExtension = new
			 * EPPExtension(); eppExtension.setExtension("abc:objUri");
			 * eppExts.add(eppExtension); eppExtension = new EPPExtension();
			 * eppExtension.setExtension("def:objUri");
			 * eppExts.add(eppExtension);
			 */
			settings.setExtensions(getExtensions());

			settingsDAO.save(settings);
		} catch (final Exception e) {
			System.out.println("Rolling back");
			outCome = "Server with the name " + friendlyName
					+ " already exists ";
			e.printStackTrace();
		}
		return "outcome";
	}

	public void validate() {
		if ((ipAddress.length() > 0) && (portNumber.length() > 0)
				&& (username.length() > 0) && password.length() > 0) {

		}
	}

	public String getFriendlyName() {
		return friendlyName;
	}

	public void setFriendlyName(final String friendlyName) {
		this.friendlyName = friendlyName;
	}

	public String getIpAddress() {
		return ipAddress;
	}

	public void setIpAddress(final String ipAddress) {
		logger.info("Setter for ip address called now");
		this.ipAddress = ipAddress;
	}

	public String getPortNumber() {
		return portNumber;
	}

	public void setPortNumber(final String portNumber) {
		logger.info("Setter for portnumber called now");
		this.portNumber = portNumber;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(final String username) {
		logger.info("Setter for username called now");
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(final String password) {
		logger.info("Setter for password called now");
		this.password = password;
	}

	public void setOutCome(final String outCome) {
		this.outCome = outCome;
	}

	public String getOutCome() {
		return outCome;
	}

	@Override
	public String toString() {
		final StringBuilder builder = new StringBuilder();
		builder.append("EPPDServerSettingsAction [friendlyName=")
				.append(friendlyName).append(", ipAddress=").append(ipAddress)
				.append(", portNumber=").append(portNumber)
				.append(", username=").append(username).append(", password=")
				.append(password).append(", outCome=").append(outCome)
				.append(", jksFileName=").append(jksFileName)
				.append(", pcksFileName=").append(pcksFileName)
				.append(", pcksFileContentType=").append(pcksFileContentType)
				.append("]");
		return builder.toString();
	}

	public void setJks(final File jksFile) {
		this.jksFile = jksFile;
	}

	public File getJksFile() {
		return jksFile;
	}

	public void setPcks(final File pcksFile) {
		this.pcksFile = pcksFile;
	}

	public File getPcksFile() {
		return pcksFile;
	}

	public String getPcksFileName() {
		return pcksFileName;
	}

	public void setPcksFileName(final String pcksFileName) {
		this.pcksFileName = pcksFileName;
	}

	public void setPcksFileContentType(final String pcksFileContentType) {
		this.pcksFileContentType = pcksFileContentType;
	}

	public void setJksFileContentType(final String jksFileContentType) {
		this.jksFileContentType = jksFileContentType;
	}

	public String getJksFileName() {
		return jksFileName;
	}

	public void setJksFileName(final String jksFileName) {
		this.jksFileName = jksFileName;
	}

	public List<String> getExtensionsList() {
		return EPPSupportedExtensions.getList();
	}

	public String getExtensions() {
		return extensions;
	}

	public void setExtensions(final String extensions) {
		this.extensions = extensions;
	}

	public void setSettingsDAO(final SettingsDAO settingsDAO) {
		this.settingsDAO = settingsDAO;
	}

}
